package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.app.dao.BsEmployeeDao;
import io.renren.modules.app.dao.BsMemberDao;
import io.renren.modules.app.dao.BsRegisaccidentDao;
import io.renren.modules.app.entity.*;
import io.renren.modules.app.service.BsRegisaccidentService;
import io.renren.modules.sys.dao.SysUserDao;
import io.renren.modules.sys.entity.SysUserEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;



@Service("bsRegisaccidentService")
@Slf4j
public class BsRegisaccidentServiceImpl extends ServiceImpl<BsRegisaccidentDao, BsRegisaccidentEntity> implements BsRegisaccidentService {

    @Resource
    private BsRegisaccidentDao bsRegisaccidentDao ;

    @Resource
    private BsMemberDao bsMemberDao ;

    @Resource
    private SysUserDao sysUserDao ;

    @Resource
    private BsEmployeeDao bsEmployeeDao ;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        LambdaQueryWrapper<BsRegisaccidentEntity> wrapper1 = new LambdaQueryWrapper<>();

        if (params.get("memberName") != null && !params.get("memberName").toString().equals("")){
            LambdaQueryWrapper<BsMemberEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(BsMemberEntity::getMemberName,params.get("memberName"));
            List<BsMemberEntity> bsMemberEntities = bsMemberDao.selectList(queryWrapper);
            List<String> list = new ArrayList<>();
            if (bsMemberEntities.size() > 0){
                for (BsMemberEntity bsMemberEntity : bsMemberEntities) {
                    list.add(bsMemberEntity.getMemberId());
                }
                wrapper1.in(BsRegisaccidentEntity::getMemberId,list);
            }else {
                IPage<BsRegisaccidentEntity> page = this.page(
                        new Query<BsRegisaccidentEntity>().getPage(params),
                        new LambdaQueryWrapper<BsRegisaccidentEntity>().eq(BsRegisaccidentEntity::getId,-1)
                );
                return new PageUtils(page);
            }

        }
        if (params.get("accidUserName") != null && !params.get("accidUserName").toString().equals("")){
            LambdaQueryWrapper<BsEmployeeEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(BsEmployeeEntity::getEmployeeName,params.get("accidUserName"));
            List<BsEmployeeEntity> userEntityList = bsEmployeeDao.selectList(wrapper);
            if (userEntityList.size() > 0){
                List<String> list = new ArrayList<>() ;
                for (BsEmployeeEntity sysUserEntity : userEntityList) {
                    list.add(sysUserEntity.getEmployeeId().toString());
                }
                wrapper1.in(BsRegisaccidentEntity::getAccidUserId,list);
            }else {
                IPage<BsRegisaccidentEntity> page = this.page(
                        new Query<BsRegisaccidentEntity>().getPage(params),
                        new LambdaQueryWrapper<BsRegisaccidentEntity>().eq(BsRegisaccidentEntity::getId,-1)
                );
                return new PageUtils(page);
            }
        }
        if (params.get("accidType") != null && !params.get("accidType").toString().equals("")){
            wrapper1.like(BsRegisaccidentEntity::getAccidType,params.get("accidType"));
        }
        if (params.get("accidLevel") != null && !params.get("accidLevel").toString().equals("")){
            wrapper1.eq(BsRegisaccidentEntity::getAccidLevel,params.get("accidLevel"));
        }

        IPage<BsRegisaccidentEntity> page = this.page(
                new Query<BsRegisaccidentEntity>().getPage(params),
                wrapper1
        );

        for (BsRegisaccidentEntity record : page.getRecords()) {
            //会员姓名数据清洗
            LambdaQueryWrapper<BsMemberEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BsMemberEntity::getMemberId,record.getMemberId());
            List<BsMemberEntity> bsMemberEntities = bsMemberDao.selectList(wrapper);
            if (bsMemberEntities.size() == 1){
                record.setMemberName(bsMemberEntities.get(0).getMemberName());
            }
            List<BsEmployeeEntity> entityList = bsEmployeeDao.selectList(
                    new LambdaQueryWrapper<BsEmployeeEntity>()
                            .eq(BsEmployeeEntity::getEmployeeId, record.getAccidUserId()));
            if (entityList.size() > 0){
                record.setAccidUserName(entityList.get(0).getEmployeeName());
            }

        }


        return new PageUtils(page);
    }

    @Override
    public void saveRegisaccid(BsRegisaccidentEntity bsRegisaccident) {
        try {
            //会员编号存在性校验
            LambdaQueryWrapper<BsMemberEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BsMemberEntity::getMemberId,bsRegisaccident.getMemberId());
            List<BsMemberEntity> entities = bsMemberDao.selectList(wrapper);
            if (entities.size() < 1){
                throw new RRException("会员编号不合法");
            }
            //值班护工存在性校验
            List<BsEmployeeEntity> entityList = bsEmployeeDao.selectList(
                    new LambdaQueryWrapper<BsEmployeeEntity>()
                            .eq(BsEmployeeEntity::getEmployeeId, bsRegisaccident.getAccidUserId()));
            if (entityList == null){
                throw new RRException("请确认值班护工编号");
            }
            bsRegisaccidentDao.insert(bsRegisaccident);
        }catch (Exception e){
            log.info("报错所在的类及方法：{}","BsRegisaccidentService --> saveRegisaccid");
            log.info(e.getMessage());
            throw new RRException(e.getMessage());
        }
    }

    @Override
    public void updateRegisaccid(BsRegisaccidentEntity bsRegisaccident) {
        try {
            BsRegisaccidentEntity entity = bsRegisaccidentDao.selectById(bsRegisaccident.getId());
            if (!entity.getMemberId().equals(bsRegisaccident.getMemberId())){
                throw new RRException("会员编号不可修改");
            }
            //值班护工存在性校验
            List<BsEmployeeEntity> entityList = bsEmployeeDao.selectList(
                    new LambdaQueryWrapper<BsEmployeeEntity>()
                            .eq(BsEmployeeEntity::getEmployeeId, bsRegisaccident.getAccidUserId()));
            if (entityList == null){
                throw new RRException("请确认值班护工编号");
            }
            bsRegisaccidentDao.updateById(bsRegisaccident);
        }catch (Exception e){
            log.info("报错所在的类及方法：{}","BsRegisaccidentService --> updateRegisaccid");
            log.info(e.getMessage());
            throw new RRException(e.getMessage());
        }
    }

}
